package com.luych.toolbox.base.service.dao;

import com.luych.toolbox.base.service.entity.Letter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

public interface LetterDao extends JpaRepository<Letter, String>, JpaSpecificationExecutor<Letter> {

    Page<Letter> findByReadedIsTrue(Pageable pageable);

    Page<Letter> findByReadedIsFalse(Pageable pageable);

    @Modifying
    @Query("update Letter set readed = true where readed = false and userId = :userId")
    @Transactional
    int setAllReaded(String userId);

    @Modifying
    @Query("update Letter set readed = false where readed = true and userId = :userId")
    @Transactional
    int setAllUnread(String userId);

    @Modifying
    @Query("delete from Letter where readed = true and userId = :userId")
    @Transactional
    int removeAllReaded(String userId);

    @Modifying
    @Query("delete from Letter where readed = false and userId = :userId")
    @Transactional
    int removeAllUnread(String userId);

    @Modifying
    @Query("delete from Letter where userId = :userId")
    @Transactional
    int removeAll(String userId);
}